<?php
function sendGcmMessage($message, $child_num) {
	//write php code.
	$gcmUrl = "https://android.googleapis.com/gcm/send";
	$serverKey = "AIzaSyDQgNx_VAMo17vSPIc6X9XrXuX-tNfXEtg";

	//for JSON
	$headers = array('Authorization:key=' . $serverKey, 'Content-Type: Application/json');

	//construct message to GCM Server.
	$sendData = array();
	$sendData['data'] = array();
	$sendData['data']['message'] = $message;
	$sendData['registration_ids'] = array();
	
	//set reg_ids to gcm data
	$sql = "SELECT reg_id FROM CHILD WHERE child_num='$child_num'";
	$result = mysql_query($sql);
	$i = 0;
	while ($row = mysql_fetch_array($result)) {
		$sendData['registration_ids'][$i++] = $row['reg_id'];
	};
	
	//php post
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendData));
	$response = curl_exec($ch);
	
	echo $response;
	if (strpos($response,'InvalidRegistration') !== false) {
		$sql = "delete from child where child_num = '$child_num'";
		exit();
	}else{
		
	}
	
	curl_close($ch);
}
?>
